<?PHP

class LinkList
{
    private $_list      = array();
    private $_style;

    public function LinkList($style = null)
    {
        $this->_style = $style;
    }

    public function Links()
    {
        return $this->_list;
    }

    public function SimpleEcho()
    {
        foreach($this->_list as $link) {
            $link->EchoLink();
        }
    }

    public function ListEcho()
    {
        if(empty($this->_style))
            echo "            <ul>\n";
        else
            echo "            <ul class='$this->_style'>\n";
        foreach($this->_list as $link) {
            $link->EchoListLink();
        }
        echo"             </ul>\n";
    }

    // return a named link by name
    public function Link($name)
    {
        return isset($this->_list[$name])
            ? $this->_list[$name]
            : null;
    }

    public function AddNamedLink($name, $caption, $url, $title = null)
    {
        $link = new Link($caption, $url, $title);
        $this->_list[$name] = $link;
    }

    public function AddLink($caption, $url)
    {
        $link = new Link($caption, $url);
        $this->_list[] = $link;
    }

    public function SetLinkStyle($key, $style)
    {
        if(isset($this->_list[$key])) {
            $this->_list[$key]->SetStyle($style);
        }
    }
}

class Link
{
    private $_name;
    private $_caption;
    private $_url;
    private $_style;
    private $_title; // for hints

    public function Link($caption, $url, $title = null)
    {
        $this->_caption = $caption;
        $this->_url = $url;
        $this->_title = $title;
    }

    public function SetStyle($style)
    {
        $this->_style = $style;
    }

    public function Name()
    {
        return $this->_name;
    }

    public function Caption()
    {
        return $this->_caption;
    }

    public function Url()
    {
        return $this->_url;
    }

    private function _link()
    {
        $lnk = "<a href='$this->_url'";
        if(!empty($this->_title))
            $lnk .= " title='{$this->_title}'";
        $lnk .= ">$this->_caption</a>";
        return $lnk;
    }

    public function EchoLink()
    {
        echo "\n            ".$this->_link();
    }

    public function EchoListLink()
    {
        if(! empty($this->_style))
            echo "            <li class='$this->_style'>{$this->_link()}</li>\n";
        else
            echo "            <li>{$this->_link()}</li>\n";
    }
}
?>
